package com.example.morton;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.viewpageindicator.R;

import java.util.ArrayList;
import java.util.List;

public class MortonPagerView extends FragmentActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {

    private List<View> viewList = new ArrayList<>();
    private ViewPager mortonPager;
    private List<String> titleList = new ArrayList<>();
    private List<Fragment> fragmentList;
    private Button mLoginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_morton_pager_view);

        mortonPager = (ViewPager) findViewById(R.id.morton_pager);
        View view1 = getLayoutInflater().inflate(R.layout.pager_view_1, null);
        View view2 = getLayoutInflater().inflate(R.layout.pager_view_2, null);
        View view3 = getLayoutInflater().inflate(R.layout.pager_view_3, null);
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);

        titleList.add("第一页");
        titleList.add("第二页");
        titleList.add("第三页");

//        mortonPager.setAdapter(new MyViewPagerAdapter(viewList, titleList));

        fragmentList = new ArrayList<>();
        fragmentList.add(new MortonFragment1());
        fragmentList.add(new MortonFragment2());
        fragmentList.add(new MortonFragment3());

        mortonPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList, titleList));
        mortonPager.setOnPageChangeListener(this);

        mLoginButton = (Button) findViewById(R.id.btn_morton_pager_login);
        mLoginButton.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_morton_pager_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Toast.makeText(this, "第" + (position + 1) + "个页面", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        Intent intent = null;
        switch (id) {
            case R.id.btn_morton_pager_login:
                intent = new Intent(MortonPagerView.this, TabListButtonActivity.class);
                break;
            default:
                break;
        }
        startActivity(intent);
    }
}
